<?php
namespace App\Services;

use App\Models\DealerStat;
use App\Models\RollStat;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class StatService
{
    private $dealerStatModel;

    private $rollStat;

    public function __construct(DealerStat $dealerStat, RollStat $rollStat)
    {
        $this->dealerStatModel = $dealerStat;
        $this->rollStat = $rollStat;
    }

    /**
     * 获取数据
     *
     * @param $saleQmTotal
     * @param $saleWinTotal
     * @param $saleTbQmTotal
     * @param $saleTbWinTotal
     * @param $saleHbQmTotal
     * @param $saleHbWinTotal
     * @return array
     */
    public function getData($saleQmTotal, $saleWinTotal, $saleTbQmTotal, $saleTbWinTotal, $saleHbQmTotal, $saleHbWinTotal): array
    {
        return [
            'sale_qm_total'    => round($saleQmTotal, 2),
            'sale_win_total'    => round($saleWinTotal, 2),
            'sale_qm_rate'     => $this->getRate($saleQmTotal, $saleTbQmTotal),
            'sale_win_rate'    => $this->getRate($saleWinTotal, $saleTbWinTotal),
            'sale_hb_qm_rate'    => $this->getRate($saleQmTotal, $saleHbQmTotal),
            'sale_hb_win_rate'     => $this->getRate($saleWinTotal, $saleHbWinTotal),
        ];
    }

    /**
     * 获取环比日期
     *
     * @return array
     */
    public function getHbDate(): array
    {
        $hb_end_date = ((int)date('Y') - 1) . '-12';
        $hb_start_date = date('Y-m-d', strtotime($hb_end_date) - (strtotime(date('Y-m-d')) - strtotime(date("Y") . '-01')));
        return [
            'start_date' => $hb_start_date,
            'end_date'   => $hb_end_date
        ];
    }

    /**
     * 经销商销售卷数
     *
     * @param int $userType
     * @return array
     */
    public function storeSales(int $userType): array
    {
        $saleTotal = $this->dealerStatModel->getSalesData(date('Y') . '-01', date('Y-m-d'), $userType);
        $saleQmTotal = $saleTotal['qm_total'];
        $saleWinTotal = $saleTotal['win_total'];

        $lastYear = (int)date('Y') - 1;
        $tb_start_date = $lastYear . '-01';
        $tb_end_date = $lastYear . '-' . date('m-d');
        $tbTotal = $this->dealerStatModel->getSalesData($tb_start_date, $tb_end_date, $userType);
        $saleTbQmTotal = $tbTotal['qm_total'];
        $saleTbWinTotal = $tbTotal['win_total'];

        $hbTotal = $this->dealerStatModel->getSalesData($this->getHbDate()['start_date'], $this->getHbDate()['end_date'], $userType);
        $saleHbQmTotal = $hbTotal['qm_total'];
        $saleHbWinTotal = $hbTotal['win_total'];

        return $this->getData($saleQmTotal, $saleWinTotal, $saleTbQmTotal, $saleTbWinTotal, $saleHbQmTotal, $saleHbWinTotal);
    }

    /**
     * 经销商销售分数
     *
     * @param int $userType
     * @return array
     */
    public function scoreSales(int $userType): array
    {
        $start_date = date('Y') . '-01';
        $end_date = date('Y-m-d');
        $saleQmTotal = $this->dealerStatModel->getScore($start_date, $end_date, 1, $userType);
        $saleWinTotal = $this->dealerStatModel->getScore($start_date, $end_date, 2, $userType);

        $lastYear = (int)date('Y') - 1;
        $tb_start_date = $lastYear . '-01';
        $tb_end_date = $lastYear . '-' . date('m-d');
        $saleTbQmTotal = $this->dealerStatModel->getScore($tb_start_date, $tb_end_date, 1, $userType);
        $saleTbWinTotal = $this->dealerStatModel->getScore($tb_start_date, $tb_end_date, 2, $userType);

        $saleHbQmTotal = $this->dealerStatModel->getScore($this->getHbDate()['start_date'], $this->getHbDate()['end_date'], 1, $userType);
        $saleHbWinTotal = $this->dealerStatModel->getScore($this->getHbDate()['start_date'], $this->getHbDate()['end_date'], 2, $userType);

        return $this->getData($saleQmTotal, $saleWinTotal, $saleTbQmTotal, $saleTbWinTotal, $saleHbQmTotal, $saleHbWinTotal);
    }

    /**
     * 获取比值
     *
     * @param $total
     * @param $compareTotal
     * @return float|int
     */
    public function getRate($total, $compareTotal)
    {
        return $compareTotal > 0 ? round(($total - $compareTotal) / $compareTotal * 100, 2) : 0;
    }

    public function rollStockAll()
    {
        $stock_all = $this->rollStat->getRollStock(1);
        $stock_dist = $this->rollStat->getRollStock(2);
        $stock_dealer = $this->rollStat->getRollStock(3);

        return [
            'stock_all_1'   => $stock_all['stock_1_total'],
            'stock_all_2'   => $stock_all['stock_2_total'],
            'stock_dist_1'   => $stock_dist['stock_1_total'],
            'stock_dist_2'   => $stock_dist['stock_2_total'],
            'stock_dealer_1'   => $stock_dealer['stock_1_total'],
            'stock_dealer_2'   => $stock_dealer['stock_2_total'],
        ];
    }

    public function rollTransfer($type)
    {
        $start_date = date('Y') . '-01';
        $end_date = date('Y-m-d');
        $transfer_all = $this->rollStat->getRollTransfer($start_date, $end_date, $type);

        $lastYear = (int)date('Y') - 1;
        $tb_start_date = $lastYear . '-01';
        $tb_end_date = $lastYear . '-' . date('m-d');
        $transfer_tb_all = $this->rollStat->getRollTransfer($tb_start_date, $tb_end_date, $type);

        $hb_start_date = $this->getHbDate()['start_date'];
        $hb_end_date = $this->getHbDate()['end_date'];
        $transfer_hb_all = $this->rollStat->getRollTransfer($hb_start_date, $hb_end_date, $type);

        return $this->getData($transfer_all['transfer_1_total'], $transfer_all['transfer_2_total'], $transfer_tb_all['transfer_1_total'], $transfer_tb_all['transfer_2_total'], $transfer_hb_all['transfer_1_total'], $transfer_hb_all['transfer_2_total']);
    }

    public function rollReturn(int $type)
    {
        $start_date = date('Y') . '-01';
        $end_date = date('Y-m-d');
        $return_all = $this->rollStat->getRollReturn($start_date, $end_date, $type);

        $lastYear = (int)date('Y') - 1;
        $tb_start_date = $lastYear . '-01';
        $tb_end_date = $lastYear . '-' . date('m-d');
        $return_tb_all = $this->rollStat->getRollReturn($tb_start_date, $tb_end_date, $type);

        $hb_start_date = $this->getHbDate()['start_date'];
        $hb_end_date = $this->getHbDate()['end_date'];
        $return_hb_all = $this->rollStat->getRollReturn($hb_start_date, $hb_end_date, $type);

        return $this->getData($return_all['return_1_total'], $return_all['return_2_total'], $return_tb_all['return_1_total'], $return_tb_all['return_2_total'], $return_hb_all['return_1_total'], $return_hb_all['return_2_total']);
    }

    public function rollSale(int $type)
    {
        $start_date = date('Y') . '-01';
        $end_date = date('Y-m-d');
        $sale_all = $this->rollStat->getRollSales($start_date, $end_date, $type);

        $lastYear = (int)date('Y') - 1;
        $tb_start_date = $lastYear . '-01';
        $tb_end_date = $lastYear . '-' . date('m-d');
        $sale_tb_all = $this->rollStat->getRollSales($tb_start_date, $tb_end_date, $type);

        $hb_start_date = $this->getHbDate()['start_date'];
        $hb_end_date = $this->getHbDate()['end_date'];
        $sale_hb_all = $this->rollStat->getRollSales($hb_start_date, $hb_end_date, $type);

        return $this->getData($sale_all['sale_1_total'], $sale_all['sale_2_total'], $sale_tb_all['sale_1_total'], $sale_tb_all['sale_2_total'], $sale_hb_all['sale_1_total'], $sale_hb_all['sale_2_total']);
    }
}